<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="违法类型" prop="illegalType">
        <el-select v-model="queryParams.illegalType" placeholder="请选择违法类型" clearable size="small">
          <el-option
            v-for="dict in illegalTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="短信通知状态" prop="messageInformStatus">
        <el-select v-model="queryParams.messageInformStatus" placeholder="请选择短信通知状态" clearable size="small">
          <el-option
            v-for="dict in messageInformStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="语音通知状态" prop="voiceInformStatus">
        <el-select v-model="queryParams.voiceInformStatus" placeholder="请选择语音通知状态" clearable size="small">
          <el-option
            v-for="dict in voiceInformStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manage:template:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:template:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:template:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['manage:template:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="templateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="违法类型" align="center" prop="illegalType" :formatter="illegalTypeFormat" />
      <!--<el-table-column label="图标" align="center" prop="icon" />-->
      <el-table-column label="描述" align="center" prop="text" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="短信通知状态" align="center" prop="messageInformStatus" :formatter="messageInformStatusFormat" />
      <el-table-column label="语音通知状态" align="center" prop="voiceInformStatus" :formatter="voiceInformStatusFormat" />
      <!--<el-table-column label="短信通知内容" align="center" prop="messageContent" />
      <el-table-column label="语音通知内容" align="center" prop="voiceContent" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:template:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manage:template:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改图片模板管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="745px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="违法类型" prop="illegalType">
          <el-select v-model="form.illegalType" placeholder="请选择违法类型">
            <el-option
              v-for="dict in illegalTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="图标" prop="icon">
          <el-input v-model="form.icon" placeholder="请输入图标" />
        </el-form-item>-->
        <el-form-item label="描述" prop="text" style="width: 49%;display: inline-block;">
          <el-input v-model="form.text" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="排序" prop="sort" style="width: 49%;display: inline-block;">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="短信通知">
          <el-radio-group v-model="form.messageInformStatus">
            <el-radio
              v-for="dict in messageInformStatusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.messageInformStatus == 1"  label="短信通知内容" prop="messageContent">
          <el-input v-model="form.messageContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="语音通知">
          <el-radio-group v-model="form.voiceInformStatus">
            <el-radio
              v-for="dict in voiceInformStatusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="图片列表" prop="imageListObj">
					<el-upload
					  class="upload-demo"
					  :headers="headers"
					  :action="url"
					  :on-preview="handlePreview"
					  :on-remove="handleRemove"
					  :on-success="picsuccess"
					  :file-list="fileList"
					  list-type="picture">
					  <el-button size="small" type="primary">点击上传</el-button>
					  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
					</el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
	.el-dialog__body{
		max-height: 660px;
		overflow: auto;
	}
	.el-upload-list--picture .el-upload-list__item {
    width: 49%;
    display: inline-block;
   }
   .el-upload-list--picture .el-upload-list__item {
    width: 163px;
    height: 166px;
   }
   .el-upload-list--picture .el-upload-list__item-thumbnail {
    width: 146px;
    height: 146px;
   }
   .el-upload-list--picture .el-upload-list__item-status-label {
    z-index: 1;
   }
   .el-upload-list__item .el-icon-close {
   	z-index: 10;
   }
   .el-upload-list__item .el-icon-close {
	    top: 0px;
	    right: 0px;
	    opacity: 1;
	    padding: 2px;
	    font-size: 19px;
	    color: #fff;
	    background: #13ce66;
		}
		.el-upload__tip {
	    display: inline-block;
	    margin-left: 10px;
		}
</style>
<script>
import { listTemplate, getTemplate, delTemplate, addTemplate, updateTemplate, exportTemplate } from "@/api/manage/template";
import { getToken } from "@/utils/auth";

export default {
  name: "Template",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图片模板管理表格数据
      templateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 违法类型字典
      illegalTypeOptions: [],
      // 短信通知状态字典
      messageInformStatusOptions: [],
      // 语音通知状态字典
      voiceInformStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        illegalType: null,
        messageInformStatus: null,
        voiceInformStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        text:[
          {required: true, message: '请输入描述', trigger: 'blur'}
        ],
        sort:[
          {required: true, message: '请输入排序', trigger: 'blur'}
        ],
      },
      headers: { Authorization: "Bearer " + getToken() },
      url: process.env.VUE_APP_BASE_API + "/common/upload",
      dialogImageUrl: '',
      dialogVisible: false,
      imageUrl: '',
      imageListObj:[],
      fileList: [],
    };
  },
  created() {
    this.getList();
    this.getDicts("break_law_type").then(response => {
      this.illegalTypeOptions = response.data;
    });
    this.getDicts("inform_status").then(response => {
      this.messageInformStatusOptions = response.data;
    });
    this.getDicts("inform_status").then(response => {
      this.voiceInformStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询图片模板管理列表 */
    getList() {
      this.loading = true;
      listTemplate(this.queryParams).then(response => {
        this.templateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 违法类型字典翻译
    illegalTypeFormat(row, column) {
      return this.selectDictLabel(this.illegalTypeOptions, row.illegalType);
    },
    // 短信通知状态字典翻译
    messageInformStatusFormat(row, column) {
      return this.selectDictLabel(this.messageInformStatusOptions, row.messageInformStatus);
    },
    // 语音通知状态字典翻译
    voiceInformStatusFormat(row, column) {
      return this.selectDictLabel(this.voiceInformStatusOptions, row.voiceInformStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        illegalType: null,
        icon: null,
        text: null,
        sort: null,
        createTime: null,
        messageInformStatus: 0,
        voiceInformStatus: 0,
        messageContent: null,
        voiceContent: null,
        imageListObj: null,
      };
      this.imageListObj = [];
      this.fileList = [];
      this.dialogImageUrl = '';
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加图片模板管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTemplate(id).then(response => {
        this.form = response.data;    
        if(response.data.imageListObj != null){
        	this.fileList = response.data.imageListObj.map(item => {
			       return {
			         name: item,
			         url: process.env.VUE_APP_BASE_API + item
			       }
					});
        }				
        this.open = true;
        this.title = "修改图片模板管理";
      });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.imageListObj = fileList;
    },
    picsuccess(response, file, fileList) {
      console.log(response, file, fileList);      
      this.imageListObj = fileList;
    },
    handlePreview(file) {
      console.log(file);
    },
    /** 提交按钮 */
    submitForm() {
			var arr = [];
			for(var i=0;i<this.imageListObj.length;i++){
				if(this.imageListObj[i].response){
					arr.push(this.imageListObj[i].response.fileName)
				}else{
					arr.push(this.imageListObj[i].name)
				}
			}
    	this.form.imageListObj = arr;
    	console.log(this.form)
    	if(this.form.illegalType == null){
    		this.$message.error('请选择违法类型');
    		return false;
    	}
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTemplate(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTemplate(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除图片模板管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTemplate(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有图片模板管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTemplate(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
